/**
 * 
 */
package com.alstudio.module.xmpp.protocol.xmpp.parser.iq.usersaction;

import java.util.ArrayList;

import android.text.TextUtils;

import com.alstudio.module.xmpp.protocol.xmpp.ALXmppEventListener;
import com.alstudio.module.xmpp.protocol.xmpp.data.ALPhoto;
import com.alstudio.module.xmpp.protocol.xmpp.packact.ALIQ;
import com.alstudio.module.xmpp.protocol.xmpp.parser.iq.ALBaseIQParser;
import com.alstudio.module.xmpp.protocol.xmpp.parser.iq.IQParseEventHandler.IQXmlParseEventCallback;
import com.alstudio.pojo.module.user.User;
import com.alstudio.pojo.module.user.Voiceintro;
import com.alstudio.utils.formater.numeric.ALNumericUtils;
import com.alstudio.utils.log.ALLog;

/**
 * @author alonso lee
 * 
 */
public class GetUserProfileResultParser extends ALBaseIQParser implements
		IQXmlParseEventCallback {

	private int code = 0;
	private ArrayList<ALPhoto> photos;
	private String type;
	private User u;

	@Override
	public void parseIQPackage(ALIQ packact, String stream,
			ALXmppEventListener listener) throws Exception {
		// TODO Auto-generated method stub
		type = "";
		u = null;
		code = 0;
		mListener = listener;
		photos = null;
		photos = new ArrayList<ALPhoto>();
		startParse(packact, stream, this);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.alstudio.module.xmpp.protocol.xmpp.parser.iq.ALBaseIQParser#onCallback
	 * ()
	 */
	@Override
	public void onCallback() {
		// TODO Auto-generated method stub
		if (mListener != null) {
			mListener.onGetUserProfileResult(code, u);
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.alstudio.module.xmpp.protocol.xmpp.parser.iq.IQParseEventHandler.
	 * IQXmlParseEventCallback#processErrorStartTag(java.lang.String)
	 */
	@Override
	public void processErrorStartTag(String tag) {
		// TODO Auto-generated method stub
		if (tag.equals(ERROR_CODE_TAG)) {
			code = ALNumericUtils.parseInt(getAttValue("code"), 0);
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.alstudio.module.xmpp.protocol.xmpp.parser.iq.IQParseEventHandler.
	 * IQXmlParseEventCallback#processSetStartTag(java.lang.String)
	 */
	@Override
	public void processSetStartTag(String tag) {
		// TODO Auto-generated method stub

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.alstudio.module.xmpp.protocol.xmpp.parser.iq.IQParseEventHandler.
	 * IQXmlParseEventCallback#processGetStartTag(java.lang.String)
	 */
	@Override
	public void processGetStartTag(String tag) {
		// TODO Auto-generated method stub

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.alstudio.module.xmpp.protocol.xmpp.parser.iq.IQParseEventHandler.
	 * IQXmlParseEventCallback#processResultStartTag(java.lang.String)
	 */
	@Override
	public void processResultStartTag(String tag) {
		// TODO Auto-generated method stub
		if (tag.equals(TYPE_TAG)) {
			type = readText();
		} else if (tag.equals(VCARD_TAG)) {
			u = new User();
		} else if (tag.equals(NICK_TAG)) {
			// 昵称
			u.setNick(readText());
		} else if (tag.equals(SEX_TAG)) {
			// 性别
			u.setSex(readText());
		} else if (tag.equals(JID_TAG)) {
			// jid
			u.setJid(readText());
		} else if (tag.equals(LAST_ONLINE_TAG)) {
			// 性别
			// 在线时间
			u.setLastLoginTimeStamp(ALNumericUtils.parseLong(readText(), 0));
		} else if (tag.equals(AGE_TAG)) {
			// 年龄
			u.setAge(readText());
		} else if (tag.equals(PROVICE_TAG)) {
			// 省会
			u.setProvince(readText());
			ALLog.d("省" + u.getProvince());
		} else if (tag.equals(CITY_TAG)) {
			// 城市
			u.setCity(readText());
			ALLog.d("市区" + u.getCity());
		} else if (tag.equals(GLAMOUR_TAG)) {
			// 魅力
			u.setGlamour(readText());
		} else if (tag.equals(PROVICE_TAG)) {
			// 省会
			u.setProvince(readText());
		} else if (tag.equals(IN_BLACK_LIST_TAG)) {
			// 黑名单
			u.setInblacklist(ALNumericUtils.parseInt(readText(), 0) == 1 ? true
					: false);
		} else if (tag.equals(ONLINE_TAG)) {
			// 在线
			u.setOnline(TRUE.equals(readText()));
		} else if (tag.equals(IS_FRIEND_TAG)) {
			// 好友关系
			// u.setIsfriend(readText());
			String tmp = readText();
			u.setIsfriend(tmp);

			if (!TextUtils.isEmpty(tmp)) {
				if (tmp.equals("1")) {
					u.setFavorite(true);
				}
			}
		} else if (tag.equals(IS_FAVORITE_TAG)) {
			// 是否已关注
			String tmp = readText();
			if (!TextUtils.isEmpty(tmp)) {
				if (tmp.equals("1")) {
					u.setFavorite(true);
				}
			}
		} else if (tag.equals(IS_FANS_TAG)) {
			// 是否已关注
			String tmp = readText();
			if (!TextUtils.isEmpty(tmp)) {
				if (tmp.equals("1")) {
					u.setFans(true);
				}
			}
		}

		else if (tag.equals(BIRTHDAY_TAG)) {
			// 生日
			u.setBirthday(readText());
		} else if (tag.equals(SIG_TAG)) {
			// 签名
			u.setSignature(readText());
		} else if (tag.equals(BACKGROUND_TAG)) {
			// 背景图
			// u.set(readText());
		} else if (tag.equals(VOICE_TAG)) {
			// 语音
			Voiceintro v = new Voiceintro();
			v.setAudioLen(ALNumericUtils.parseInt(getAttValue("len"), 0));
			v.setVoiceFileUrl(readText());
			u.setVoiceintro(v);
		} else if (tag.equals(VAUTHED_TAG)) {
			// V认证状态
			u.setvAuthPos(ALNumericUtils.parseInt(getAttValue("index"), -1));
			u.setVauthed(ALNumericUtils.parseInt(readText(), 0));
		}

		else if (tag.equals(STATUS_TAG)) {
			// 在线状态
			u.setStatus(ALNumericUtils.parseInt(readText(), 0));
		} else if (tag.equals(FAN_SNUM_TAG)) {
			// 粉丝数
			u.setCollectingnum(readText());
		} else if (tag.equals(EMAIL_TAG)) {
			// 绑定邮箱
			u.setEmailIsBind(ALNumericUtils.parseInt(getAttValue("status"), 0));
			u.setEmail(readText());
		} else if (tag.equals(GOLD_TAG)) {
			// 金币
			u.setGold(readText());
		} else if (tag.equals(PHOTOS_TAG)) {
			// 可查看照片数
			u.setViewMaxPhoto(ALNumericUtils.parseInt(getAttValue("max"), 20));
			// 当前照片数
			u.setPhotoCount(ALNumericUtils.parseInt(getAttValue("count"), 0));
		} else if (tag.equals(PHOTO_TAG)) {
			ALPhoto p = new ALPhoto();
			p.setOrder(getAttValue("id"));
			p.setThumbnailFileid(getAttValue("small_pic"));
			p.setPicFileid(getAttValue("large_pic"));
			photos.add(p);
		} else if (tag.equals(STAR_TAG)) {
			u.setStarState(ALNumericUtils.parseInt(readText(), 0));
		} else if (tag.equals(CALL_PRICE_TAG)) {
			u.setCallPrice(readText());
			ALLog.d("呼叫价格" + u.getCallPrice());
		} else if (tag.equals("idstatus")) {
			u.setEmail_addr(getAttValue("email"));
			u.setEmailIsBind(ALNumericUtils.parseInt(getAttValue("status"), 0));
		} else if (tag.equals("blockstrangercall")) {
			// 屏蔽陌生人来电
			u.setBlockstrangercall(TRUE.equals(readText()));
		} else if (tag.equals("blockstrangermsg")) {
			// 屏蔽陌生人留言
			u.setBlockstrangermsg(TRUE.equals(readText()));
		} else if (tag.equals("")) {
			// 通话中
			u.setCalling(TRUE.equals(readText()));
		} else if (tag.equals("callnotify")) {
			// 来电调戏
			u.setCallNoticeState(TRUE.equals(readText()));
		}

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.alstudio.module.xmpp.protocol.xmpp.parser.iq.IQParseEventHandler.
	 * IQXmlParseEventCallback#processEndTag(java.lang.String)
	 */
	@Override
	public void processEndTag(String tag) {
		// TODO Auto-generated method stub
		if (tag.equals(PHOTOS_TAG)) {
			u.setPhotos(photos);
			ALLog.d("照片數目" + photos.size());
			if (photos.size() > 0) {
				u.setImageFileId(photos.get(0).getPicFileid());
			}
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.alstudio.module.xmpp.protocol.xmpp.parser.iq.IQParseEventHandler.
	 * IQXmlParseEventCallback#processStartDocument()
	 */
	@Override
	public void processStartDocument() {
		// TODO Auto-generated method stub

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.alstudio.module.xmpp.protocol.xmpp.parser.iq.IQParseEventHandler.
	 * IQXmlParseEventCallback#processEndDocument()
	 */
	@Override
	public void processEndDocument() {
		// TODO Auto-generated method stub
		onCallback();
	}

}
